package com.fenghongzhang.day013;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

public class Main2Activity extends AppCompatActivity {
    private Button send;

    private static final String TAG = "Main2Activity";
    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        send = (Button) findViewById(R.id.send);
        //开启子线程
        new MyThread().start();

        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i(TAG, "onClick: ");
                //发送消息
                if(handler != null){
                    Message obtain = Message.obtain();
                    obtain.obj = "还行.没有那么困";
                    handler.sendMessage(obtain);
                }
            }
        });
    }

    class MyThread extends Thread{
        @Override
        public void run() {
            super.run();
            Log.i(TAG, "run: ");
            //准备
            Looper.prepare();
            handler = new Handler(){
                @Override
                public void handleMessage(@NonNull Message msg) {
                    super.handleMessage(msg);
                    String o = (String) msg.obj;
                    Log.i(TAG, "handleMessage: "+o);
                }
            };
            Looper.loop();
        }
    }
}
